// 引入数据库模型
const Chat = require("../model/Chat");

// 提交消息
async function addChatMessage(params) {
  const result = await Chat.create(params);
  if (result) {
    return result;
  } else {
    return false;
  }
}
// 提交消息
async function addGroupChatMessage(params) {
  const result = await Chat.create(params);
  if (result) {
    return true;
  } else {
    return false;
  }
}
// 获取消息
async function getMessageList(params) {
  delete params.content;
  delete params._id;
  delete params.content;
  delete params.time;
  console.log(params);
  const infoArr = [params, { to: params.from, from: params.to }];
  const result = await Chat.find({ $or: infoArr });
  console.log(result);
  if (result) {
    return result;
  } else {
    return false;
  }
}
// 获取群聊消息
async function getGroupChatList(params) {
  const to = params.to;
  const result = await Chat.find({ to });
  if (result) {
    return result;
  } else {
    return false;
  }
}

// 导出
module.exports = {
  addChatMessage,
  getMessageList,
  addGroupChatMessage,
  getGroupChatList,
};
